<?php

// R0910CMS 
// created by msn
// time : 20100120
// edit by TLightSky
// time : 20100220

if(!defined("IN_R0910CMS")) {
    exit("Access Denied!");
}

/**
 *
 * @$c -> check($this->val); only check for right
 * @$c -> checkValue($this->val); check right & not empty if required.
 */
 	//include_once "dataCheck.php";
 	class R_Db_Cell {
        public  $checkFunc = null;
        public  $sqlStr    = null;

        private $defaultVal = null;
        private $val       = null;
		private $notNull   = false;
        private $primary   = false;
        private $unsigned   = false;
        private $autoIncrement   = false;
        private $comment       = null;
		
        public function check($v) {
            return call_user_func($this->checkFunc, $v);
        }
        function checkNull($v) {
            if($this->isNotNull()) {
                return R_Db_DataCheck::checkNotNull($v);
            } else {
                return true;
            }
        }
		public function setValue($v){
			if($this->check($v)){
				$this->val = $v;
				return true;
			}
			else{
                throw new R_Exception_ParamWrong('Cell value '.$v.' doesn\'t match the cell type:'. join('  ', $this->checkFunc) .'!');
			}
		}
        public function getValue() {
            return $this->val;
        }
        public function checkValue() {
            return ($this->check($this->val) 
                &&$this->checkNull($this->val));
        }

        public function getSql($myName) {
            $ret = join(" " ,array("`".$myName."`", 
                   $this->sqlStr,
                   $this->isUnsigned()?'unsigned':'' ,
                   $this->isNotNull()?'NOT NULL':'NULL' ,
                   $this->hasDefaultValue()?"DEFAULT '".$this->defaultVal."'":'' ,
                   $this->isAutoIncrement()?"AUTO_INCREMENT":'',
                   $this->hasComment()?"COMMENT '".$this->comment."'":'' ,
                   ) // end of array
            );
            return $ret;
        }

        public function setPrimary() { $this->primary = true; return $this; }
        public function isPrimary()  { return $this->primary; }
        public function setNotNull() { $this->notNull = true; return $this; }
        public function isNotNull()  { return $this->notNull; }
        public function setAutoIncrement() { $this->autoIncrement = true; return $this; }
        public function isAutoIncrement()  { return $this->autoIncrement; }
        public function setUnsigned() { $this->unsigned = true; return $this; }
        public function isUnsigned()  { return $this->unsigned; }        


        public function setEmpty() { $this->val = null; return $this; }
        public function isEmpty()  { return $this->val==null; }

        public function setDefaultValue($val) { $this->defaultVal = $val; return $this; }
        public function hasDefaultValue() { return ($this->defaultVal === null)?false:true; }
        public function setComment($comment) { $this->comment = $comment; return $this; }
        public function hasComment() { return ($this->comment === null)?false:true; }
	}
